package com.gzw.dms.activity;

import android.content.Context;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.gzw.dms.R;
import com.gzw.dms.base.BaseActivity;
import com.gzw.dms.utils.PropertiesUtil;
import com.gzw.dms.utils.SPUtils;

/**
 * Created by Wangzhiqiang on 2016/3/24.
 */
public class SetIpActivity extends BaseActivity {
    private Context mContext;
    private EditText etIp1;
    private EditText etIp2;
    private EditText etIp3;
    private EditText etIp4;
    private EditText etDkh;
    private Button btnConfirm;
    private TextView tv_text;
    private Toolbar toolbar;


    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_set_ip);
    }

    @Override
    protected void initViews() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        etIp1 = (EditText) findViewById(R.id.et_ip1);
        etIp2 = (EditText) findViewById(R.id.et_ip2);
        etIp3 = (EditText) findViewById(R.id.et_ip3);
        etIp4 = (EditText) findViewById(R.id.et_ip4);
        etDkh = (EditText) findViewById(R.id.et_dkh);
        btnConfirm = (Button) findViewById(R.id.btn_confirm);
        tv_text = (TextView) findViewById(R.id.tv_text);
    }

    @Override
    protected void initData() {
        mContext = this;
        toolbar.setTitle("IP设置");
        setSupportActionBar(toolbar);

        String url = PropertiesUtil.Read("IP");
        String strs[] = url.split("\\.");
        String strs1[] = strs[0].split("://");
        String strs2[] = strs[3].split(":");
        etIp1.setText(strs1[1]);
        etIp2.setText(strs[1]);
        etIp3.setText(strs[2]);
        etIp4.setText(strs2[0]);
        etDkh.setText(strs2[1]);
    }

    @Override
    protected void initEvents() {
        btnConfirm.setOnClickListener(new mOnClick());
        toolbar.setNavigationOnClickListener(new mOnClick());

    }

    /**
     * Created by gaozhiwei on 2015-11-02
     * 点击监听
     */
    class mOnClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
                case R.id.btn_confirm:
                    SetUrl();
                    break;
                default:
                    finish();
                    break;
            }
        }
    }

    private void SetUrl() {
        if (isNull(etIp1)) {
            showAlertDialog("提示", "请输入正确IP地址");
            return;
        }
        if (isNull(etIp2)) {
            showAlertDialog("提示", "请输入正确IP地址");
            return;
        }
        if (isNull(etIp3)) {
            showAlertDialog("提示", "请输入正确IP地址");
            return;
        }
        if (isNull(etIp4)) {
            showAlertDialog("提示", "请输入正确IP地址");
            return;
        }
        if (isNull(etDkh)) {
            showAlertDialog("提示", "请输入正确IP地址");
            return;
        }
        if (etIp1.getText().toString().trim().length() > 3 ||
                etIp2.getText().toString().trim().length() > 3 ||
                etIp3.getText().toString().trim().length() > 3 ||
                etIp4.getText().toString().trim().length() > 3 ||
                Integer.parseInt(etIp1.getText().toString().trim()) > 255 ||
                Integer.parseInt(etIp2.getText().toString().trim()) > 255 ||
                Integer.parseInt(etIp3.getText().toString().trim()) > 255 ||
                Integer.parseInt(etIp4.getText().toString().trim()) > 255) {
            showAlertDialog("提示", "请输入正确IP地址");
            return;
        }
          String aaa=etDkh.getText().toString();
        if (etDkh.getText().toString().trim().length() > 5 ||
                Integer.parseInt(etDkh.getText().toString().trim()) > 65535) {
            showAlertDialog("提示", "请输入正确的端口号");
            return;
        }
        String url = "http://" + etIp1.getText() + "." + etIp2.getText() + "." + etIp3.getText() + "." + etIp4.getText() + ":" + etDkh.getText();
        SPUtils.put(mContext, "IP", url);
        showAlertDialog("提示", "IP设置成功。");
        finish();
    }
}
